import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/config.dart';
import 'package:cloud_medicine/http/prescription_api.dart';
import 'package:cloud_medicine/model/prescription_detail.dart';
import 'package:cloud_medicine/static/sex.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class PrescriptionDetailPage extends StatefulWidget {
  final int id;

  PrescriptionDetailPage({
    Key key,
    @required this.id,
  }) : super(key: key);

  @override
  _PrescriptionDetailPageState createState() => _PrescriptionDetailPageState();
}

class _PrescriptionDetailPageState extends State<PrescriptionDetailPage> {
  bool _loading = false;
  PrescriptionDetail _info;

  @override
  void initState() {
    super.initState();
    _init();
  }

  Future<Null> _init() async {
    setState(() {
      _loading = true;
    });
    dynamic res = await getPrescriptionDetail_api(
      '$backIP/prescription',
      {'id': widget.id},
    );
    if (mounted) {
      setState(() {
        if (res['code'] == 0) {
          _info = PrescriptionDetail.init(res['data']);
        } else {
          errorToast(res['message']);
        }
        _loading = false;
      });
    }
  }

  _buildUserInfoText({String title, String value}) {
    return RichText(
      text: TextSpan(
        style: TextStyle(
            color: Colors.black, fontWeight: FontWeight.bold, fontSize: 14),
        children: <InlineSpan>[
          TextSpan(text: '$title：'),
          TextSpan(text: value, style: TextStyle(color: Colors.blue)),
        ],
      ),
    );
  }

  _buildSicknessInfo({String title, String value}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text(
          '$title：',
          style: TextStyle(color: Colors.grey),
        ),
        Expanded(
          child: Text(value),
        )
      ],
    );
  }

  _buildDivider() {
    return Divider(
      color: Colors.black,
      height: 30,
      thickness: 2,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('处方详情'),
        centerTitle: true,
      ),
      body: _loading || _info == null
          ? loadingWidget(_loading)
          : Container(
              padding: EdgeInsets.fromLTRB(10, 20, 10, 20),
              child: ListView(
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Text(
                        '云家医处方笺',
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: 24,
                        ),
                      )
                    ],
                  ),
                  _buildDivider(),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      _buildUserInfoText(title: '姓名', value: "***"),
                      _buildUserInfoText(
                          title: '性别', value: sexContrast[_info.sex]),
                      _buildUserInfoText(title: '年龄', value: _info.age),
                    ],
                  ),
                  SizedBox(height: 10),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      _buildUserInfoText(title: '医生', value: "***"),
                      _buildUserInfoText(title: '日期', value: _info.createTime),
                    ],
                  ),
                  _buildDivider(),
                  _buildSicknessInfo(title: '病情分析', value: _info.description),
                  SizedBox(height: 30),
                  _buildSicknessInfo(title: '药物推荐', value: _info.medicine),
                  _buildDivider(),
                ],
              ),
            ),
    );
  }
}
